package com.itheima.stock.controller;


import com.itheima.stock.pojo.SysUser;
import com.itheima.stock.service.UserService;
import com.itheima.stock.vo.req.LoginReqVo;
import com.itheima.stock.vo.req.PutUserRole;
import com.itheima.stock.vo.req.TianJiaReq;
import com.itheima.stock.vo.req.UserReq;
import com.itheima.stock.vo.resp.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class UserController {

    //业务层的UserService对象
    @Autowired
    private UserService userService;

    //测试访问接口
    @GetMapping("/test")
    public Object getName() {
        return "itheima";
    }

    //登录访问接口
    @PostMapping("/login")
    public R<LoginRespVo> login(@RequestBody LoginReqVo vo) {
        System.out.println(vo);
        R<LoginRespVo> login = userService.login(vo);
        return login;
    }

    //验证码访问接口
    @GetMapping("/captcha")
    public R<RespCode> generateCaptcha(){
        return userService.generateCaptcha();
    }


    //用户管理页面展示
    @PostMapping("/users")
    public R<PageResult<SysUser>> UserSX(@RequestBody UserReq userReq) {
        R<PageResult<SysUser>> all = userService.findAll(userReq.getPageNum(), userReq.getPageSize());
        return all;
    }


    //用户添加接口
    @PostMapping("/user")
    public R<TianJiaRes> Tianjia(@RequestBody TianJiaReq tianJiaReq) {
        return userService.insertUser(tianJiaReq);
    }


    //用户获取自己有什么权限
    @GetMapping("/user/roles/{userId}")
    public R<RoleRes> getRole(@PathVariable String userId) {
        R<RoleRes> role = userService.findRole(userId);
        return role;
    }


    //更新用户角色
    @PutMapping("/user/roles")
    public R<TianJiaRes> putUserRole(@RequestBody PutUserRole putUserRole) {
        R<TianJiaRes> tianJiaResR = userService.putUserRole(putUserRole);
        return tianJiaResR;
    }


    //删除用户
    @DeleteMapping("/user")
    public R<TianJiaRes> deleteUser(@RequestBody List<String> res) {
        R<TianJiaRes> tianJiaResR = userService.deleteUser(res);
        return tianJiaResR;
    }


//    //根据id查询用户信息
//    @GetMapping("/user/info/{userId}")
//    public R<FindUser> getUser(@PathVariable String userId) {
//        System.out.println(userId);
//        return null;
//    }


    //编辑用户
    @PutMapping("/user")
    public R<TianJiaRes> Bianji(@RequestBody FindUser findUser) {
        R<TianJiaRes> tianJiaResR = userService.updateUser(findUser);
        return tianJiaResR;
    }



}
